﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using System.Threading;
using System.Runtime.CompilerServices;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid;
using DevExpress.Data;

namespace QLTV.Forms.KhaiBao
{

    public partial class frmCrewList : BaseForm
    {

        public frmCrewList()
        {
            InitializeComponent();

        }


        private void frmCrewList_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dsCrew.tblSchoolName' table. You can move, or remove it, as needed.
            this.tblSchoolNameTableAdapter.Fill(this.dsCrew.tblSchoolName);
            // TODO: This line of code loads data into the 'dsVessel.vesselDetail' table. You can move, or remove it, as needed.
            this.vesselDetailTableAdapter.Fill(this.dsVessel.vesselDetail);
            // TODO: This line of code loads data into the 'dsTraining.tblTrainingCourse' table. You can move, or remove it, as needed.



            RunLongThread(loadData);

        }


        public void loadData()
        {
            this.tblTrainingCourseTableAdapter.Fill(this.dsTraining.tblTrainingCourse);
            tblLoaiHDTableAdapter1.Fill(dsCrew.tblLoaiHD);
            // TODO: This line of code loads data into the 'dsDiaChi.tblTinhThanh' table. You can move, or remove it, as needed.
            this.tblTinhThanhTableAdapter.Fill(this.dsDiaChi.tblTinhThanh);
            // TODO: This line of code loads data into the 'dsDiaChi.tblQuanHuyen' table. You can move, or remove it, as needed.
            this.tblQuanHuyenTableAdapter.Fill(this.dsDiaChi.tblQuanHuyen);
            // TODO: This line of code loads data into the 'dsDiaChi.tblPhuongXa' table. You can move, or remove it, as needed.
            this.tblPhuongXaTableAdapter.Fill(this.dsDiaChi.tblPhuongXa);
            // TODO: This line of code loads data into the 'dsCrew.tblCertification' table. You can move, or remove it, as needed.
            this.tblCertificationTableAdapter.Fill(this.dsCrew.tblCertification);
            // TODO: This line of code loads data into the 'dsCrew.tblCertificateCrew' table. You can move, or remove it, as needed.
            this.tblCertificateCrewTableAdapter.Fill(this.dsCrew.tblCertificateCrew);
            // Start my lengthy Process
            // TODO: This line of code loads data into the 'dsVessel.tblEngineType' table. You can move, or remove it, as needed.
            this.tblEngineTypeTableAdapter.Fill(this.dsVessel.tblEngineType);
            // TODO: This line of code loads data into the 'dsVessel.tblEngineMaker' table. You can move, or remove it, as needed.
            this.tblEngineMakerTableAdapter.Fill(this.dsVessel.tblEngineMaker);
            // TODO: This line of code loads data into the 'dsVessel.tblVesselType' table. You can move, or remove it, as needed.
            this.tblVesselTypeTableAdapter.Fill(this.dsVessel.tblVesselType);
            // TODO: This line of code loads data into the 'dsVessel.tblFlag' table. You can move, or remove it, as needed.
            this.tblFlagTableAdapter.Fill(this.dsVessel.tblFlag);
            // TODO: This line of code loads data into the 'dsVessel.tblVessel' table. You can move, or remove it, as needed.
            this.tblVesselTableAdapter.Fill(this.dsVessel.tblVessel);
            // TODO: This line of code loads data into the 'dsCrew.tblCourse' table. You can move, or remove it, as needed.
            this.tblCourseTableAdapter.Fill(this.dsCrew.tblCourse);
            // TODO: This line of code loads data into the 'dsCrew.tblEducationType' table. You can move, or remove it, as needed.
            this.tblEducationTypeTableAdapter.Fill(this.dsCrew.tblEducationType);
            // TODO: This line of code loads data into the 'dsCrew.tblImmigrationDoc' table. You can move, or remove it, as needed.
            this.tblImmigrationDocTableAdapter.Fill(this.dsCrew.tblImmigrationDoc);
            // TODO: This line of code loads data into the 'dsRanking.tblRank' table. You can move, or remove it, as needed.
            this.tblRankTableAdapter.Fill(this.dsRanking.tblRank);
            tblViewCrewTableAdapter1.Fill(dsCrew.tblViewCrew);
            _wait.ShouldCloseNow = true;
        }
        QLTV.Database.dsCrewTableAdapters.tblNextIDTableAdapter tadNextID = new Database.dsCrewTableAdapters.tblNextIDTableAdapter();

        public string getCrewID()
        {

            tadNextID.Fill(dsCrew.tblNextID);
            tblCrewTableAdapter.Fill(dsCrew.tblCrew);
            //kiem tra next id o bang temp
            if (dsCrew.tblNextID.Rows.Count > 0)
            {
                var data = from crew in dsCrew.tblNextID.AsEnumerable()
                           orderby Convert.ToInt32(crew.Field<string>("NextID")) descending
                           select Convert.ToInt32(crew.Field<string>("NextID"));

                int maxID = data.First<int>();

                if (maxID > 0 && maxID < 10)
                {
                    return "000" + (maxID + 1);

                }
                if (maxID >= 10 && maxID < 100)
                {
                    return "00" + (maxID + 1);

                }
                if (maxID >= 100 && maxID < 1000)
                {
                    return "0" + (maxID + 1);

                }
                if (maxID >= 1000)
                {
                    return maxID.ToString();

                }
            }
            else
            {
                if (dsCrew.tblCrew.Rows.Count > 0)
                {
                    var data = from crew in dsCrew.tblCrew.AsEnumerable()
                               orderby Convert.ToInt32(crew.Field<string>("nCrewId").Substring(1)) descending
                               select Convert.ToInt32(crew.Field<string>("nCrewId").Substring(1));

                    int maxID = data.First<int>();

                    if (maxID > 0 && maxID < 10)
                    {
                        return "000" + (maxID + 1).ToString();

                    }
                    if (maxID >= 10 && maxID < 100)
                    {
                        return "00" + (maxID + 1).ToString();

                    }
                    if (maxID >= 100 && maxID < 1000)
                    {
                        return "0" + (maxID + 1).ToString();

                    }
                    if (maxID >= 1000)
                    {
                        return maxID.ToString();

                    }
                }
                else
                    return "0001";
            }
            return "";
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int rowHandle = viewCrew.FocusedRowHandle;
            frmCrew f = new frmCrew();
            f.StartPosition = FormStartPosition.CenterParent;
            f.isNew = true;
            f.newCrewid = getCrewID();
            //thêm nextid
            tadNextID.Insert(f.newCrewid);
            f.ShowDialog();
            tblViewCrewTableAdapter1.Fill(dsCrew.tblViewCrew);
            //            tblCrewBindingSource.DataSource = tblViewCrewTableAdapter1.GetData();
            viewCrew.FocusedRowHandle = rowHandle;
            loadDataCrew();

        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            int rowHandle = viewCrew.FocusedRowHandle;
            if (viewCrew.FocusedRowHandle >= 0)
            {

                frmCrew f = new frmCrew();
                f.StartPosition = FormStartPosition.CenterParent;
                f.Text = String.Format("{0}: {1} {2} {3}", rm.GetString("frmCrewTitle", myCultureInfo), viewCrew.GetFocusedRowCellValue("cFamilyName"), viewCrew.GetFocusedRowCellValue("cMiddleName"), viewCrew.GetFocusedRowCellValue("cFirstName"));
                f.isNew = false;
                f.crewid = viewCrew.GetFocusedRowCellValue("nCrewId").ToString();
                f.ShowDialog();
                tblViewCrewTableAdapter1.Fill(dsCrew.tblViewCrew);
                //  tblCrewBindingSource.DataSource = tblViewCrewTableAdapter1.GetData();
                viewCrew.SelectRow(rowHandle);
                viewCrew.FocusedRowHandle = rowHandle;

                loadDataCrew();

            }
        }
        void loadDataCrew()
        {
            if (viewCrew.FocusedRowHandle > -1)
            {

                tblImmigrationCrewBindingSource.DataSource = tblImmigrationCrewTableAdapter.GetDataByCrewID(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());
                tblEducationBindingSource.DataSource = tblEducationTableAdapter.GetDataByCrewID(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());
                tblExprienceBindingSource.DataSource = this.tblExprienceTableAdapter.GetDataBy(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());
                tblNhanXetCuaChuTauBindingSource.DataSource = tblNhanXetCuaChuTauTableAdapter.GetDataByCrewID(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());
                tblCertificateCrewBindingSource.DataSource = tblCertificateCrewTableAdapter.GetDataByCrewID(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());

                tblQuanHeBindingSource.DataSource = this.tblQuanHeTableAdapter.GetDataBy(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());
                tblHDLDBindingSource.DataSource = tblHDLDTableAdapter.GetDataBy(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());
                tblAccountCrewBindingSource.DataSource = this.tblAccountCrewTableAdapter.GetDataBy(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());
                tblTrainingCrewsBindingSource.DataSource = tblTrainingCrewsTableAdapter.GetDataByCrew(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());

                QLTV.Database.AssignmentDataContext data = new Database.AssignmentDataContext();
                QLTV.Database.tblAssignment a = data.tblAssignments.SingleOrDefault(a1 => a1.nCrewid == viewCrew.GetFocusedRowCellValue(colnCrewId) && a1.isActive == true);
                if (a != null)
                {

                    switch (a.cStatus.ToUpper())
                    {
                        case "ON BOARD":
                            labelControl1.Text = "Tình trạng: đang trên tàu";
                            labelControl1.Text += "\n\r Ngày xuống tàu: " + a.dEmbarked.Value.ToString("dd/MM/yyyy");
                            labelControl1.Text += String.Format("\n\r Thời gian hợp đồng: {0} tháng", a.nContract.Value);
                            labelControl1.Text += "\n\r Ngày kết thúc hợp đồng: " + a.dDuKienRoiTau.Value.ToString("dd/MM/yyyy");

                            lblGhiChu.Text = String.Format("{0}: {1}", rm.GetString("sNote", myCultureInfo), a.cGhiChu);

                            break;
                        case "ON VACATION":
                            labelControl1.Text = "Tình trạng: đang nghỉ";
                            labelControl1.Text += "\n\r Ngày rời tàu: " + a.dLeave.Value.ToString("dd/MM/yyyy");
                            labelControl1.Text += String.Format("\n\r Thời gian yêu cầu nghỉ: {0} tháng", a.nRequest.Value);
                            labelControl1.Text += "\n\r Ngày dự kiến lên tàu: " + a.dSanSangLenTau.Value.ToString("dd/MM/yyyy");

                            lblGhiChu.Text = String.Format("{0}: {1}", rm.GetString("sNote", myCultureInfo), a.cGhiChu);

                            break;
                        case "ON RESIGN":
                            labelControl1.Text = "Tình trạng: kết thúc hợp đồng";
                            labelControl1.Text += "\n\r Ngày kết thúc hợp đồng: " + a.dCatHopDong.Value.ToString("dd/MM/yyyy");

                            lblGhiChu.Text = String.Format("{0}: {1}", rm.GetString("sNote", myCultureInfo), a.cGhiChu);

                            break;
                        case "ON RETIRE":
                            labelControl1.Text = "Tình trạng: On retire";
                            labelControl1.Text += "\n\r Ngày nghỉ hưu: " + a.dNghiHuu.Value.ToString("dd/MM/yyyy");

                            lblGhiChu.Text = String.Format("{0}: {1}", rm.GetString("sNote", myCultureInfo), a.cGhiChu);

                            break;
                        case "NEW CREW":
                            labelControl1.Text = "Tình trạng: mới tuyển";
                            labelControl1.Text += "\n\r Ngày ký hợp đồng: " + a.dKyHopDong.Value.ToString("dd/MM/yyyy");
                            lblGhiChu.Text = String.Format("{0}: {1}", rm.GetString("sNote", myCultureInfo), a.cGhiChu);

                            break;
                        default:
                            break;
                    }
                }
            }
        }
        private void viewRank_FocusedRowChanged_1(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            loadDataCrew();
        }



        private void btnXem_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (viewNhanXet.FocusedRowHandle > -1 && viewNhanXet.GetFocusedRowCellValue("pNoiDung") != null)
            {

                string ext = viewNhanXet.GetFocusedRowCellValue("cExt").ToString();
                string filename = String.Format("{0}{1}{2}", Path.GetTempPath(), DateTime.Now.Ticks, ext);

                FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
                BinaryWriter br = new BinaryWriter(fs);
                br.Write((byte[])viewNhanXet.GetFocusedRowCellValue("pNoiDung"));
                fs.Dispose();
                System.Diagnostics.Process.Start(filename);

            }
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (viewCrew.FocusedRowHandle > -1)
            {
                if (XtraMessageBox.Show(rm.GetString("sConfirm", myCultureInfo), rm.GetString("sMessageCaption", myCultureInfo), MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    //   tblCrewBindingSource.RemoveCurrent();
                    tblCrewTableAdapter.DeleteCrew(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());
                    tblViewCrewTableAdapter1.Fill(dsCrew.tblViewCrew);
                }
            }
        }

        private void gridCrew_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                popupMenu1.ShowPopup(new Point(Cursor.Position.X, Cursor.Position.Y));
            }
        }
        private void barXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (viewCrew.FocusedRowHandle > -1)
            {
                if (XtraMessageBox.Show(rm.GetString("cCaption", myCultureInfo), "Thông báo", MessageBoxButtons.OKCancel) == System.Windows.Forms.DialogResult.OK)
                {
                    viewCrew.SetFocusedRowCellValue("bDeleted", true);
                    tblCrewBindingSource.EndEdit();
                    tblCrewTableAdapter.Update(dsCrew.tblCrew);
                }
            }
        }

        private void btnUndo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (viewCrew.FocusedRowHandle > -1)
            {
                viewCrew.SetFocusedRowCellValue("bDeleted", false);
                tblCrewBindingSource.EndEdit();
                tblCrewTableAdapter.Update(dsCrew.tblCrew);
            }
        }

        private void cmdDo_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

            if (viewCrew.FocusedRowHandle > -1)
            {
                List<string> crewsId = new List<string>();
                int position = viewCrew.FocusedRowHandle;
                int[] rowsHandle = viewCrew.GetSelectedRows();
                foreach (int i in rowsHandle)
                {
                    crewsId.Add(viewCrew.GetRowCellValue(i, colnCrewId).ToString());
                }
                frmAssinment f = new frmAssinment();
                f.ucAssignment1.crewList = crewsId;
                f.ShowDialog();
                // tblViewCrewTableAdapter1.Fill(dsCrew.tblViewCrew);
                tblCrewBindingSource.DataSource = tblViewCrewTableAdapter1.GetData();
                // tblCrewBindingSource.Position = position;

            }
        }

        private void radStatus_EditValueChanged(object sender, EventArgs e)
        {
            
            switch (((RadioGroup)(sender)).EditValue.ToString())
            {
                case "all":
                    tblCrewBindingSource.Filter = "cStatus='On Board' or cStatus='On Vacation' or cStatus='New Crew'";
                    break;
                case "onboard":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='On Board'";
                    break;
                case "onretire":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='On Retire'";
                    break;
                case "onvacation":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='On Vacation'";
                    break;
                case "onresign":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='On Resign'";
                    break;
                case "newcrew":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='New Crew'";
                    break;
                case "notin":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='On Resign' or cStatus='On Retire'";
                    break;
            }
        }

        private void viewHoChieu_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            if (e.Column == colSTT && e.RowHandle > -1)
            {
                e.DisplayText = Convert.ToString(e.RowHandle + 1);
            }
        }

        private void viewCrew_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {

            GridView currentView = sender as GridView;
            if (e.RowHandle == currentView.FocusedRowHandle) return;
            if (e.RowHandle > -1)
            {
                string status = currentView.GetRowCellValue(e.RowHandle, currentView.Columns["cStatus"]).ToString().ToUpper();
                switch (status)
                {
                    case "NEW CREW":
                        if (e.Column == colNgayKyHD)
                        {
                            e.Appearance.BackColor = Color.Yellow;
                            e.Appearance.ForeColor = Color.Black;

                        }
                        break;

                    case "ON RESIGN":
                        if (e.Column == colNgayCatHD)
                        {
                            e.Appearance.BackColor = Color.Red;
                            e.Appearance.ForeColor = Color.White;

                        }
                        break;
                    case "ON RETIRE":
                        if (e.Column == colNgayNghiHuu)
                        {
                            e.Appearance.BackColor = Color.Red;
                            e.Appearance.ForeColor = Color.White;

                        }

                        break;
                    case "ON VACATION":
                        if (e.Column == colNgaySS || e.Column == colRoiTau || e.Column == colThoiGianYcNghi)
                        {
                            e.Appearance.BackColor = Color.Yellow;
                            e.Appearance.ForeColor = Color.Black;

                        }

                        break;
                    case "ON BOARD":
                        if (e.Column == colTenTau || e.Column == colEmbarked || e.Column == colDuKienRoiTau)
                        {
                            e.Appearance.BackColor = Color.Yellow;
                            e.Appearance.ForeColor = Color.Black;

                        }

                        break;
                    default:
                        break;
                }

            }

        }




        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (viewCrew.FocusedRowHandle > -1)
            {
                frmContractList f = new frmContractList(viewCrew.GetFocusedRowCellValue("nCrewId").ToString());
                f.MdiParent = this.MdiParent;
                f.Show();
            }
        }


        private void repositoryItemButtonEdit2_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (viewHoChieu.FocusedRowHandle > -1 && viewHoChieu.GetFocusedRowCellValue("bScanDoc") != DBNull.Value)
            {
                string ext = viewHoChieu.GetFocusedRowCellValue("cExt").ToString();
                string filename = String.Format("{0}{1}{2}", Path.GetTempPath(), DateTime.Now.Ticks, ext);

                FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
                BinaryWriter br = new BinaryWriter(fs);
                br.Write((byte[])viewHoChieu.GetFocusedRowCellValue("bScanDoc"));
                fs.Dispose();
                System.Diagnostics.Process.Start(filename);

            }
        }

        private void repositoryItemButtonEdit3_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (viewEducation.FocusedRowHandle > -1 && viewEducation.GetFocusedRowCellValue("bScanDoc") != DBNull.Value)
            {
                string ext = viewEducation.GetFocusedRowCellValue("cExt").ToString();
                string filename = String.Format("{0}{1}{2}", Path.GetTempPath(), DateTime.Now.Ticks, ext);

                FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
                BinaryWriter br = new BinaryWriter(fs);
                br.Write((byte[])viewEducation.GetFocusedRowCellValue("bScanDoc"));
                fs.Dispose();
                System.Diagnostics.Process.Start(filename);

            }
        }

        private void repositoryItemButtonEdit4_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (viewCertificate.FocusedRowHandle > -1 && viewCertificate.GetFocusedRowCellValue("bScanDoc") != DBNull.Value)
            {
                string ext = viewCertificate.GetFocusedRowCellValue("cExt").ToString();
                string filename = String.Format("{0}{1}{2}", Path.GetTempPath(), DateTime.Now.Ticks, ext);

                FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
                BinaryWriter br = new BinaryWriter(fs);
                br.Write((byte[])viewCertificate.GetFocusedRowCellValue("bScanDoc"));
                fs.Dispose();
                System.Diagnostics.Process.Start(filename);

            }
        }

        private void viewCrew_DoubleClick(object sender, EventArgs e)
        {
            int rowHandle = viewCrew.FocusedRowHandle;
            if (viewCrew.FocusedRowHandle >= 0)
            {

                frmCrew f = new frmCrew();
                f.StartPosition = FormStartPosition.CenterParent;
                f.Text = String.Format("{0}: {1} {2} {3}", rm.GetString("frmCrewTitle", myCultureInfo), viewCrew.GetFocusedRowCellValue("cFamilyName"), viewCrew.GetFocusedRowCellValue("cMiddleName"), viewCrew.GetFocusedRowCellValue("cFirstName"));
                f.isNew = false;
                f.crewid = viewCrew.GetFocusedRowCellValue("nCrewId").ToString();
                f.ShowDialog();
                tblViewCrewTableAdapter1.Fill(dsCrew.tblViewCrew);
                viewCrew.SelectRow(rowHandle);
                viewCrew.FocusedRowHandle = rowHandle;

            }
        }
        int soNguoiPhuThuoc;

        private void viewPhuThuoc_CustomSummaryCalculate(object sender, DevExpress.Data.CustomSummaryEventArgs e)
        {
            int summaryID = Convert.ToInt32((e.Item as GridSummaryItem).Tag);
            GridView View = sender as GridView;

            // Initialization 
            if (e.SummaryProcess == CustomSummaryProcess.Start)
            {
                soNguoiPhuThuoc = 0;

            }
            // Calculation 
            if (e.SummaryProcess == CustomSummaryProcess.Calculate)
            {
                if (View.GetRowCellValue(e.RowHandle, "Kết thúc") == DBNull.Value || (DateTime)View.GetRowCellValue(e.RowHandle, "Kết thúc") > DateTime.Now.Date)
                    switch (summaryID)
                    {
                        case 1: // The total summary calculated against the 'UnitPrice' column. 
                            soNguoiPhuThuoc++;
                            break;

                    }
            }
            // Finalization 
            if (e.SummaryProcess == CustomSummaryProcess.Finalize)
            {
                switch (summaryID)
                {
                    case 1:
                        e.TotalValue = soNguoiPhuThuoc;
                        break;

                }
            }
        }



        private void viewPhuThuoc_CustomDrawCell(object sender, RowCellCustomDrawEventArgs e)
        {
            if (e.Column == stt)
            {
                e.DisplayText = Convert.ToString(e.RowHandle + 1);
            }
        }

        private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (viewCrew.FocusedRowHandle > -1)
            {
                string crewID = viewCrew.GetFocusedRowCellValue("nCrewId").ToString();
                Reports.rptCrewCard _crewCard = new Reports.rptCrewCard(crewID);
                _crewCard.ShowPreview();
            }
        }

        private void barButtonItem9_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveFileDialog s = new SaveFileDialog();
            s.FileName = "Crew List";
            s.Filter = "Excel files (*.xls)|*.xls";
            DevExpress.XtraPrinting.XlsExportOptions op = new DevExpress.XtraPrinting.XlsExportOptions();
            if (s.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                viewCrew.ExportToXls(s.FileName);
                if (MessageBox.Show("Do you want to open it?", "Crew Management System", MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    System.Diagnostics.Process.Start(s.FileName);
            }
        }

        private void viewCrew_CustomUnboundColumnData(object sender, CustomColumnDataEventArgs e)
        {
            
        }

        private void viewCrew_CustomColumnDisplayText(object sender, CustomColumnDisplayTextEventArgs e)
        {
            if (e.Column == crewSTT && e.RowHandle > -1)
            {
                e.DisplayText = Convert.ToString(e.RowHandle + 1);
            }
        }

        private void radNotIn_EditValueChanged(object sender, EventArgs e)
        {
            switch (((RadioGroup)(sender)).EditValue.ToString())
            {
                case "allnotin":
                    tblCrewBindingSource.Filter = "cStatus='On Retire' or cStatus='On Resign'";
                    break;
           
                case "onretire":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='On Retire'";
                    break;
                case "onresign":
                    tblCrewBindingSource.Filter = "";
                    tblCrewBindingSource.Filter = "cStatus='On Resign'";
                    break;
               
            }
        }

        private void viewPhuThuoc_CalcPreviewText(object sender, CalcPreviewTextEventArgs e)
        {
            e.PreviewText = "Ghi chú: " + e.PreviewText;
        }

        private void viewHDLD_CalcPreviewText(object sender, CalcPreviewTextEventArgs e)
        {
            e.PreviewText = "Ghi chú: " + e.PreviewText;
        }
    }
}
